<?php
namespace Attendance\Controller;
use Common\Controller\AdminbaseController;
use Home\Service\ClockTimeService;
use Home\Service\AccountService;
// +----------------------------------------------------------------------
// | Date:2017年3月1日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class AnnualController extends AdminbaseController{
    public function _initialize(){
        parent::_initialize();
        $this->model = D('AnnualLeave');
    }
    
    public function index(){
        $sn = I('sn');
        $name = I('name');
        if ($name)  {
            $account = new AccountService();
            $userinfo = $account->get_user_by_field(['ep_name'=>$name]);
            $sn =$userinfo['sn'];
            $where['name'] = ['like',"%$name%"];
        }
        if ($sn)  $where['sn'] = $sn;
        
        
        $list = $this->lists($this->model,$where);
        
        $account = new AccountService();
        $list = $account->append_user_field('sn',$list,'sn,ep_name,ep_id,join_date');
        
        $this->assign('list',$list);
        $this->display();
    }
    
    /**
     * 导出上一年度的数据
     *
     * date:2018年1月2日
     * author: EK_熊
     */    
    public function export_last(){
        $year = date('Y',strtotime('-1 year'));
        $this->index_print($year);
    }
    
    /**
     * 导出本年度的数据
     * 
     * date:2018年1月2日
     * author: EK_熊
     */
    public function export_cur(){
        $year = date('Y');
        $this->index_print($year);        
    }
    
    /**
     * 导出数据
     * 
     * date:2018年1月2日
     * author: EK_熊
     */
    public function index_print($year=''){
        
        if ($year) $where["DATE_FORMAT(start_date,'%Y')"] =$year; 
      //  $list = $this->model->where($where)->order('start_date desc')->group('sn')->select();
        $list = $this->model->where($where)->order('sn')->select();
        $account = new AccountService();
        $list = $account->append_user_field('sn',$list,'sn,ep_name,ep_id,join_date,status'); 
        $this->assign('list',$list);
//        $this->display('index_print');
        $this->show_print('index',"{$year}年年假数据表");
    }
    
    public function edit(){
        $id = I('id');
        
        if(IS_POST){
            $post = I('post');
            if ($post['sick_full_remain'] > 1440) $this->error('全日薪病假不能超过1440分钟');
            if ($post['sick_half_remain'] > 960) $this->error('全日薪病假不能超过960分钟');
          //  $post['total_min'] = $post['total_day']*480;
            $post['adjust'] = $post['adjust']*480;
            unset($post['total_day']);
            $ret = $this->model->where(['id'=>$id])->save($post);
            if (!$ret) $this->error('操作失败！');
            $this->success('操作完成！！');
        }
        
        $info = $this->model->where(['id'=>$id])->find();
        $account = new AccountService();
        $userinfo = $account->get_user_by_field(['sn'=>$info['sn']]);
        $info['name'] =$userinfo['ep_name'];
        $info['total_day'] =$info['total_min']/480;
        $info['adjust'] =$info['adjust']/480;
        $this->assign('info',$info);
        $this->display();
    }
    
    
    
    
}